app.controller("z-announcement-c", [ '$scope','$rootScope','$z-http','$sce','z-announcement-s','z-edit-menu-s','$filter','$location','downloadUrl',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,$filter,$location,$downloadUrl) {
		$scope.baseUrl = $location.protocol()+"://"+$location.host()+":"+$location.port()+$downloadUrl;
	
		$scope.viewModel = {};
		$scope.editModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";
		//编辑使用
		$scope.editModel = {};
		$scope.editModel.title="";
		// 获取当天日期
		$scope.editModel.expireDate = new Date();
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'announcement'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getAnnouncementList({
				pageCount : $scope.viewModel.perPage,
				pageNo : $scope.viewModel.rowCount,
				title:$scope.viewModel.title,
				isDeactivate:$scope.viewModel.isDeactivate,
			},
			function(data){
				$scope.viewModel.dataList = data.announcementBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//新增
		$scope.viewModel.add = function(){
			$scope.viewModel.load();
			$scope.editModel.type = true;
			$scope.editModel.title="新增公告";
			$scope.editModel.announcement={};
			$scope.editModel.announcement.content="";
			$scope.editModel.announcement.attachmentList=[];
			// 上传控件
            $scope.importModel.uploadStream.cancel();
			
			//验证不能为空
			$scope.editForm.title.$dirty=false;
			//$scope.editForm.content.$dirty=false;
			$scope.editForm.expireDate.$dirty=false;
		};
		
		//修正
		$scope.viewModel.update=function(announcement){
			$scope.viewModel.isLoading=true;
			Service.getAnnouncementById({
				id:announcement.id,
			},function(data){
				$scope.viewModel.isLoading=false;
				$scope.editModel.announcement=data;
				if(announcement.expireDate!=null){
					$scope.editModel.announcement.expireDate=new Date(announcement.expireDate);
				}
				$scope.editModel.type = false;
				$scope.editModel.title="修改公告";
				$scope.viewModel.load();
				$scope.importModel.uploadStream.cancel();
			},function(error){
				$scope.viewModel.isLoading=false;
				$rootScope.alertError(error.fault.message);
			});
		};
		
		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.title.$dirty){
            		$scope.editForm.title.$dirty=true;
            	}
            	/*if(!$scope.editForm.content.$dirty){
            		$scope.editForm.content.$dirty=true;
            	}*/
            	if(!$scope.editForm.expireDate.$dirty){
            		$scope.editForm.expireDate.$dirty=true;
            	}
                return true;
            }else{
            	if($scope.editModel.announcement.content==""){
            		$rootScope.alertWarning("请输入内容信息。");
            		return true;
            	}else{
            		return false;
            	}
            }
        };
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveAnnouncement($scope.editModel.announcement,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("新增成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}else{
				Service.updateAnnouncement($scope.editModel.announcement,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("修改成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}
		};
		// 停用
		$scope.viewModel.stop = function(announcement){
			$rootScope.alertConfirm("确定是否停用吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.isDeactivateAnnouncement({
					id:announcement.id,
					isDeactivate:"1",
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("停用成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		}
		// 启用
		$scope.viewModel.start = function(announcement){
			$rootScope.alertConfirm("确定是否启用吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.isDeactivateAnnouncement({
					id:announcement.id,
					isDeactivate:"0",
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("启用成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		}
		// 删除
		$scope.viewModel.del = function(announcement){
			$rootScope.alertConfirm("确定是否删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteAnnouncement({
					id:announcement.id
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		}
		
		//获取富文本框内容
		$scope.editModel.conHtml = function(content){
			$scope.editModel.announcement.content = content;
		};
		
		//日期控件
		$scope.editModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		
		//日期控件
		$scope.editModel.format = "yyyy/MM/dd";
		
		//日期启动开关
		$scope.editModel.effectiveDateOpen = false;
		$scope.editModel.effectiveDateOpnened = false;
		$scope.editModel.effectiveDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.editModel.effectiveDateOpnened = !$scope.editModel.effectiveDateOpnened;		
		};
		
        // 附件删除
        $scope.editModel.delAttachment = function (attachment) {
        	$rootScope.alertConfirm("确定删除吗？",function() {
				for(var i = 0; i < $scope.editModel.announcement.attachmentList.length; i++){
					if($scope.editModel.announcement.attachmentList[i].attachmentId == attachment.attachmentId){
						$scope.editModel.announcement.attachmentList.splice(i,1);
					}
				}
			});
        };
		
		/***********************附件**************************/
		$scope.importModel = {};
        $scope.importModel.uploadStream = {};
        $scope.importModel.attachmentId = null;
        $scope.importModel.fileName = null;
        $scope.importModel.url = null;

        // 上传
        $scope.importModel.fileUploadComplete = function (data) {
            var dataList=data;
            if (dataList[0]['attachment'] != null) {
                $scope.$apply(function () {
                    $scope.importModel.attachmentId = dataList[0]['attachment']['attachmentId'];
                    $scope.importModel.fileName = dataList[0]['attachment']['fileName'];
                    $scope.importModel.url = dataList[0]['attachment']['url'];
                    $scope.importModel.save();
                });
            }else{
            	$rootScope.alertWarning("请先选择文件。");
            }
        };
        
        // 下载
		$scope.importModel.download = function (url,fileName) {
			const ele = document.createElement('a');
		    ele.setAttribute('href',url); //设置下载文件的url地址
		    ele.setAttribute('download',fileName);//用于设置下载文件的文件名
		    ele.click();
        };
		
        // 保存
        $scope.importModel.save = function () {
        	var date={};
        	date.attachmentId=$scope.importModel.attachmentId;
        	date.fileName= $scope.importModel.fileName;
        	date.url=$scope.importModel.url;
        	$scope.editModel.announcement.attachmentList.push(date);
        	
            $scope.importModel.attachmentId = null;
            $scope.importModel.fileName = null;
            $scope.importModel.url = null;
            $scope.importModel.uploadStream.cancel();
        };
		/****************************************************/
} ]);